#include<iostream>
using namespace std;
const int N = 1e4 + 10;
int sz[N];
int p[N];
int d[N];
int n, m;
int find(int x)
{
	if (p[x] != x) {
		int root = find(p[x]);
		d[x] += d[p[x]];
		p[x] = root;
	}
	return p[x];
}
int main()
{
	int t;
	cin >> t;
	int T = 1;
	while (t--) {
		scanf("%d%d", &n, &m);
		for (int i = 1; i <= n; i++) {
			p[i] = i;
			sz[i] = 1;
			d[i] = 0;
		}
		cout << "Case " << T << ":" << endl;
		while (m--) {
			char op[2];
			scanf("%s", op);
			if (op[0] == 'T') {
				int a, b;
				scanf("%d%d", &a, &b);
				a = find(a);
				b = find(b);
				if (a != b) {
					p[a] = b;
					sz[b] += sz[a];
					d[a]++;
				}
			}
			else {
				int x, x1;
				scanf("%d", &x);
				x1 = find(x);
				printf("%d %d %d\n", x1, sz[x1], d[x]);
			}
		}
		T++;
	}
	return 0;
}